package org.penguin.study.bk.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Locale;

/**
 * @author chengyinghua
 */
@Slf4j
@Controller
@RequestMapping("/i18n")
public class HelloI8nController {

    @Autowired
    private MessageSource messageSource;

    @RequestMapping(value = "/hello", method = {RequestMethod.GET, RequestMethod.POST})
    public String hello(@RequestParam(defaultValue = "无缺") String fullName, @RequestParam(defaultValue = "1") Boolean gender, Locale locale, Model model) {
        log.debug("姓名:{}, 性别:{}", fullName, gender);
        model.addAttribute("helloMessage", messageSource.getMessage("hello.message", null, locale));
        model.addAttribute("hello", String.format("%s%s，请您收下来自程英华的问候", fullName, Boolean.TRUE.equals(gender) ? "先生" : "女士"));
        return "hello";
    }
}
